package com.house.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.house.entity.House;
import com.house.entity.HouseImage;
import com.house.entity.UserInf;
import com.house.mapper.HouseImageMapper;
import com.house.mapper.HouseMapper;
import com.house.util.PageRequest;
import com.house.util.PageResult;
import com.house.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseService {

    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private HouseImageMapper houseImageMapper;

    public House getById(int id){


        return houseMapper.selectByPrimaryKey(id);
    }

    public List<HouseImage> findByHouseId(int houseID){

        return houseImageMapper.findByHouseId(houseID);
    }
    public List<House> findAll(){
        return houseMapper.findAll();
    }
    public List<House> findAllRelease(){
        return houseMapper.findAllRelease();
    }
    public PageResult getAll(PageRequest pageRequest){
        return PageUtils.getPageResult(pageRequest, getAllPageInfo(pageRequest));
    }
    public int countbyArea(String area){
        return houseMapper.countbyArea(area);
    }
    public PageResult getAllUnRealse(PageRequest pageRequest){
        return PageUtils.getPageResult(pageRequest, getAllUnRealsePageInfo(pageRequest));
    }

    private PageInfo<House> getAllPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<House> houses = houseMapper.findAll();
        return new PageInfo<House>(houses);
    }

    private PageInfo<House> getAllUnRealsePageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<House> houses = houseMapper.findAllUnRelease();
        return new PageInfo<House>(houses);
    }
    public List<House> findByPay(String pay){
        return houseMapper.findByPay(pay);
    }

    public List<House> findBy(String paytype,String type,String area){
        return  houseMapper.findBy(paytype,type,area);
    }
    public List<House> findByOrder(String paytype,String type,String area,String order ){
        return  houseMapper.findByOrder(paytype,type,area,order);
    }

    public List<House> findAllByStatus(int status){

        return houseMapper.findAllByStatus(status);

    }

    public void updateById(House house){
        houseMapper.updateByPrimaryKey(house);
    }

    public void updateStatusByIdAndStatus(int id,int status){
        houseMapper.updateStatusByIdAndStatus(id,status);
    }

    public void deleteById(int id){
        houseMapper.deleteByPrimaryKey(id);
    }

    public List<House> findAllByUserId(int id){
        return houseMapper.findAllByUserId(id);
    }
    public int insertHouse(House house){
        return houseMapper.insert(house);
    }
}
